package com.dragon.graduation.userRole.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.config.exception.exception.GlobalException;
import com.dragon.graduation.userRole.controller.dto.UserRoleAddDTO;
import com.dragon.graduation.userRole.controller.dto.UserRoleSearchDTO;
import com.dragon.graduation.userRole.controller.dto.UserRoleUpdateDTO;
import com.dragon.graduation.userRole.controller.vo.UserRoleVO;
import com.dragon.graduation.userRole.entity.UserRoleEntity;
import com.dragon.graduation.userRole.service.UserRoleService;
import com.dragon.results.page.ResultPage;
import com.dragon.results.result.HttpResult;
import com.dragon.util.MyPage;
import com.dragon.util.WrapperUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户角色表 前端控制器
 * </p>
 *
 * @author dragon
 * @since 2025-02-28 21:23:58
 */
@RestController
@RequestMapping("/userRole")
@RequiredArgsConstructor
@Api(tags = "用户角色表 相关接口")
public class UserRoleController {

    private final UserRoleService userRoleService;

    @PostMapping("add")
    @ApiOperation("1]用户角色表添加")
    @ApiOperationSupport(order = 1)
    public HttpResult<String> add(@RequestBody UserRoleAddDTO addDTO) {
        userRoleService.removeByRoleId(addDTO.getRoleId());
        List<UserRoleEntity> entityList=new ArrayList<>();
        for (Integer userId : addDTO.getUserId()) {
            UserRoleEntity build = UserRoleEntity.builder()
                    .roleId(addDTO.getRoleId())
                    .userId(userId)
                    .build();
            entityList.add(build);
        }
        if (!CollectionUtils.isEmpty(entityList)) {
            userRoleService.saveBatch(entityList);
        }
        return HttpResult.ok("绑定成功");
    }

    @PostMapping("update")
    @ApiOperation("2]用户角色表修改")
    @ApiOperationSupport(order = 2)
    public HttpResult<String> update(@RequestBody UserRoleUpdateDTO updateDTO) {
        UserRoleEntity entity = userRoleService.getById(updateDTO.getId());
        Assert.isFalse(ObjectUtils.isEmpty(entity), () -> new GlobalException("要修改的数据不存在"));
        entity = UserRoleUpdateDTO.to(updateDTO, UserRoleEntity.class);
        userRoleService.updateById(entity);
        return HttpResult.ok("修改成功");
    }

    @PostMapping("search")
    @ApiOperation("3]用户角色表分页查询")
    @ApiOperationSupport(order = 3)
    public HttpResult<ResultPage<List<UserRoleVO>>> search(@RequestBody UserRoleSearchDTO searchDTO) {
        QueryWrapper<UserRoleEntity> queryWrapper = WrapperUtil.constructWrapper(searchDTO, UserRoleEntity.class);

        Page<UserRoleEntity> entityPage = MyPage.setPage(UserRoleEntity.class, searchDTO.getPage());
        Page<UserRoleEntity> page = userRoleService.page(entityPage, queryWrapper);
        ResultPage<List<UserRoleVO>> resultPage = MyPage.copyPage(page, UserRoleVO.class);
        return HttpResult.okForData(resultPage);
    }

    @PostMapping("delete")
    @ApiOperation("4]用户角色表删除")
    @ApiOperationSupport(order = 4)
    public HttpResult<String> delete(@RequestBody List<Integer> idList) {
        userRoleService.removeByIds(idList);
        return HttpResult.ok("删除成功");
    }

    @GetMapping("find/role")
    @ApiOperation("5]根据角色id获取用户id")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParam(name = "roleId", value = "角色id",dataTypeClass = Integer.class)
    public HttpResult<List<Integer>> findByRoleId(Integer roleId) {
        List<UserRoleEntity> listed = userRoleService.lambdaQuery()
                .eq(UserRoleEntity::getRoleId, roleId)
                .list();
        List<Integer> result=new ArrayList<>();
        if (!CollectionUtils.isEmpty(listed)) {
            listed.forEach(item -> {
                result.add(item.getUserId());
            });
        }
        return HttpResult.okForData(result);
    }
}
